package licode.unisop.client.controller;

import licode.unisop.client.info.*;
import licode.unisop.client.utils.MethodCallUtil;
import licode.unisop.client.utils.SopResult;
import licode.unisop.client.visit.UserVisit;
import licode.unisop.client.vo.SopUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author licode
 */
@RestController
@RequestMapping("/uni-api/v1/user")
public class SopUserController extends SopBaseController {
    @Resource
    private UserVisit userVisit;

    @PostMapping("/cache/update")
    void updateCache(@RequestBody SopUserIdIn userId) {
        userVisit.updateCache(userId);
    }

    /**
     * 修改用户密码
     * @param info 账号信息
     */
    @RequestMapping(value = "/password/update", method = RequestMethod.POST)
    public SopResult<String> updatePassword(@RequestBody SopUpdatePasswordIn info) {
        return MethodCallUtil.exec("", () -> userVisit.updatePassword(info));
    }

    /**
     * 重置用户密码
     * @param info 账号信息
     */
    @RequestMapping(value = "/password/reset", method = RequestMethod.POST)
    public void resetPassword(@RequestBody SopResetPasswordIn info) {
        userVisit.resetPassword(info);
    }

    @PostMapping("/save")
    public SopResult<SopUser> saveUser(@RequestBody SopUser user) {
        return MethodCallUtil.exec(null, () -> userVisit.save(user));
    }

    @PostMapping("/account/save")
    public SopResult<Boolean> saveAccount(@RequestBody SopAccountIn accountIn) {
        return MethodCallUtil.exec(false, () -> userVisit.saveAccount(accountIn));
    }

    @PostMapping("/identity/save")
    public SopResult<Boolean> saveIdentity(@RequestBody SopIdentityIn identityIn) {
        return MethodCallUtil.exec(false, () -> userVisit.saveIdentity(identityIn));
    }
}
